version: 2
jobs:
  build-macos:
    macos:
      xcode: 12.4.0
    steps:
      - checkout
      - run:
          name: Submodules
          command: git submodule update --init --recursive

      - run:
          name: Install Go 1.10.8
          command: |
            curl https://dl.google.com/go/go1.10.8.darwin-amd64.tar.gz -o go1.10.8.darwin-amd64.tar.gz
            sudo tar -C /usr/local -xzf go1.10.8.darwin-amd64.tar.gz
            echo 'export GOROOT=/usr/local/go' >> $BASH_ENV
            echo 'export GOPATH=$HOME/go' >> $BASH_ENV
            echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> $BASH_ENV

      - run:
          name: Install Node 10.24.1
          command: |
            echo "Uninstalling preinstalled version of Node"
            brew uninstall --ignore-dependencies node
            brew uninstall --force node

            echo "Installing NVM"
            HOMEBREW_NO_AUTO_UPDATE=1 brew install nvm
            export NVM_DIR="$HOME/.nvm"
            mkdir $NVM_DIR

            echo "Activating NVM"
            [ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"

            echo "Installing Node"
            nvm install 10.24.1
            nvm use 10.24.1
            nvm alias default 10.24.1

            echo "Setting up Node env"
            echo 'export NVM_DIR="$HOME/.nvm"' >> $BASH_ENV
            echo '[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh"' >> $BASH_ENV

      # We need to use a newer cacert bundle with git to make "go get" commands trust gopkg.in
      # This is because the circleci images don't backport security fixes (ofc),
      # and Lets Encrypt root cert expired on September 31st, 2021.
      - run:
          name: Update cacerts
          command: |
            HOMEBREW_NO_AUTO_UPDATE=1 brew install curl
            HOMEBREW_NO_AUTO_UPDATE=1 brew upgrade git
            HOMEBREW_NO_AUTO_UPDATE=1 brew upgrade openssl
            export PATH="/usr/local/opt/curl/bin:$PATH"
            echo 'PATH="/usr/local/opt/curl/bin:$PATH"' >> $BASH_ENV
            sudo curl https://curl.se/ca/cacert.pem -o /etc/ssl/curl-cacert.pem
            git config --global http.sslCAInfo /etc/ssl/curl-cacert.pem

      - run:
          name: Debug
          command: |
            node --version
            npm --version

            npm i -g yarn
            yarn --version
            yarn cache clean

      # We can't run with node >= 12 anyways, so this is irrelevant.
      # # Ignoring engines because @typescript/estlint requires node>=12,
      # # but atm we can't upgrade because appdmg is broken (and abandoned).
      # # TODO: fix this properly
      # - run: yarn install --ignore-engines

      # For some reason, ignoring engines causes node gyp compilation errors with fsevents.
      - run: yarn install

      - run: yarn run client-build-osx
      # To make a test package, comment out the previous line and uncomment this line.
      # - run: yarn run client-build-osx --useTestPackage

      - store_artifacts:
          path: ./build/prod-client-build/osx64-package.tar.gz
      - store_artifacts:
          path: ./build/prod-client-build/GameJoltClient.dmg

  build-linux:
    docker:
      - image: circleci/node:14.17
    steps:
      - checkout
      - run:
          name: Submodules
          command: git submodule update --init --recursive

      - run:
          name: Update and upgrade
          command: |
            sudo apt-get update
            sudo apt-get upgrade

      - run:
          name: Install OpenGL dev dependencies
          command: sudo apt-get install -y libgl1-mesa-dev xorg-dev

      - run:
          name: Install Go 1.10.8
          command: |
            wget https://dl.google.com/go/go1.10.8.linux-amd64.tar.gz
            sudo tar -C /usr/local -xzf go1.10.8.linux-amd64.tar.gz
            echo 'export GOROOT=/usr/local/go' >> $BASH_ENV
            echo 'export GOPATH=$HOME/go' >> $BASH_ENV
            echo 'export PATH=$PATH:$GOROOT/bin:$GOPATH/bin' >> $BASH_ENV

      - run: yarn install
      - run: yarn run client-build-linux64
      # To make a test package, comment out the previous line and uncomment this line.
      # - run: yarn run client-build-linux64 --useTestPackage

      - store_artifacts:
          path: ./build/prod-client-build/linux64-package.tar.gz
      - store_artifacts:
          path: ./build/prod-client-build/GameJoltClient.tar.gz
workflows:
  version: 2
  build_all:
    jobs:
      - build-macos:
          filters:
            branches:
              only:
                - client-build
      - build-linux:
          filters:
            branches:
              only:
                - client-build
